package com.android.happymall.fragment.detail;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.happymall.R;
import com.android.happymall.fragment.BaseLazyFragment;
import com.android.happymall.model.detail.MainDetail;
import com.android.happymall.utils.ObjectUtils;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * 商品详情页
 */
public class Fragment_Detail_Goods extends BaseLazyFragment {
    @BindView(R.id.pager)
    ViewPager pager;
    @BindView(R.id.close)
    ImageView close;
    @BindView(R.id.top)
    ImageView top;
    @BindView(R.id.indicator)
    TextView indicator;
    @BindView(R.id.progress_loading)
    FrameLayout progressLoading;

    private FragmentActivity mcontext;
    private View view;
    private LayoutInflater inflater;
    private int type;
    private int detailid;
    private BannerAdapt adapter_banner;
    private boolean networkConnected;
    private String storeid;

    public Fragment_Detail_Goods() {
    }
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM3 = "param3";
    private MainDetail response;
    private ArrayList<String> Banner = new ArrayList<>();

    public static Fragment_Detail_Goods newInstance(MainDetail param1, int iskaipin) {
        Fragment_Detail_Goods fragment = new Fragment_Detail_Goods();
        Bundle args = new Bundle();
        args.putSerializable(ARG_PARAM1, param1);
        args.putInt(ARG_PARAM3, iskaipin);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            response = (MainDetail) getArguments().getSerializable(ARG_PARAM1);
        }
    }

    @Override
    protected void initPrepare() {
    }

    private void initLayout() {
        initBanner();
    }



    private void initBanner() {
        adapter_banner = new BannerAdapt();
        pager.setAdapter(adapter_banner);
        pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                CharSequence text = getString(R.string.viewpager_indicator,
                        position + 1, pager.getAdapter().getCount());
                indicator.setText(text);
            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    protected void onInvisible() {

    }

    @Override
    protected void initData() {
        GetDetailData();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected View initView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        this.inflater = inflater;
        view = inflater.inflate(R.layout.activity_refund_detail, container, false);
        mcontext = getActivity();
        ButterKnife.bind(this, view);
        initLayout();
        return view;
    }

    private void GetDetailData() {
        type = response.getData().getTypeid();
        detailid = response.getData().getId();
        String team_price = response.getData().getTeam_price();
        SpannableString msp = new SpannableString("￥" + team_price);
        msp.setSpan(new AbsoluteSizeSpan(13, true), 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        if (response.getData().getThumbs() != null && response.getData().getThumbs().size() > 0) {
            Banner.addAll(response.getData().getThumbs());
            adapter_banner.notifyDataSetChanged();
        } else {

        }
    }

    @OnClick({R.id.close, R.id.top})
    public void onClick(View view) {
        switch (view.getId()) {


            case R.id.close:
                mcontext.finish();
                break;

            case R.id.top:


                break;
        }
    }


    private class BannerAdapt extends PagerAdapter {

        @Override
        public int getCount() {
            return Banner.size();
        }


        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            View view = inflater.inflate(R.layout.item_fragment_banner, null);
            ImageView photoview = (ImageView) view.findViewById(R.id.imageView1);
            ObjectUtils.photo2(mcontext, Banner.get(position), photoview);
            container.addView(view);
            return view;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }
    }

}
